<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>01 - 光源的 target 属性</title>
    <!-- 引入三维引擎 -->
    <script src="../external/three-js-r93/build/three.js"></script>
    <!-- 扩展控件 OrbitControls -->
    <script src="../external/three-js-r93/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<script>

    let scene = new THREE.Scene();

    let geometry = new THREE.BoxGeometry(30, 30, 30);

    let material = new THREE.MeshLambertMaterial({
       color: 0xff0000
    });


    let mesh1 = new THREE.Mesh(geometry, material);
    scene.add(mesh1);

    mesh1.translateX(-20);
    mesh1.translateZ(20);
    mesh1.rotateX(2);


    let geometry2 = new THREE.BoxGeometry(20, 20, 20);

    let material2 = new THREE.MeshLambertMaterial({
        color: 0xffff00
    });

    let mesh2 = new THREE.Mesh(geometry2, material2);
    /* 复制 mesh 的位置、旋转、矩阵等属性，不包含 geometry 和 material 属性 */
    mesh2.copy(mesh1);
    /* 相比 mesh1 而言 在平移 */
    mesh2.translateX(40);
    scene.add(mesh2);

    let mesh3 = mesh2.clone();/* 浅拷贝 拷贝的是 geometry 和 material 的引用 拷贝双方共享材质和几何体数据 */
    mesh3.material.color = new THREE.Color(0x0000ff);
    mesh3.translateX(40);
    scene.add(mesh3);


    let axis = new THREE.Vector3(2, 10, 1);

    console.log(axis);
    axis.normalize();//向量归一化
    console.log(axis);

    // 沿着 axis 平移100
    mesh2.translateOnAxis(axis, 100);

    /* 添加辅助坐标 */
    let axesHelper = new THREE.AxesHelper(200);
    scene.add(axesHelper);

    //点光源
    let point = new THREE.SpotLight(0xffffff);
    point.position.set(400, 200, 300); //点光源位置
    // point.target = mesh1;
    scene.add(point); //点光源添加到场景中
    //环境光
    let ambient = new THREE.AmbientLight(0x444444);
    scene.add(ambient);

    /* 添加相机 */
    let width = window.innerWidth;
    let height = window.innerHeight;
    let k = width / height; // 窗口宽高比
    let s = 150; // 三维场景显示范围控制系数， 系数越大，显示的范围越大

    let camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    camera.position.set(200, 300, 200);
    camera.lookAt(scene.position);

    /* 渲染器 */
    let renderer = new THREE.WebGLRenderer({antialis: true, alpha:true});// 抗锯齿
    renderer.setSize(width, height);//渲染区域尺寸
    renderer.setClearColor(0xC4C4C4, 0.1);//渲染背景色

    document.body.appendChild(renderer.domElement); // body 元素中插入 canvas 对象

    /*渲染函数*/
    function render() {

        renderer.render(scene, camera);

    }


    render();

    /* 添加控件对象 */
    let controls = new THREE.OrbitControls(camera, renderer.domElement);// 控件可以监听鼠标的变化，改变相机的属性
    controls.addEventListener('change', render);//添加鼠标监听，触发渲染函数，更新 canvas 画布渲染效果

</script>

</body>
</html>